

package cell

import (
	"github.com/treelab/updatecolumn/models"
)

// InstanceType of cell
func InstanceType() int32 {
	return models.Number
}


 DEFAULT_PRECISION = 0

  cellValueToString({ cell, column }: { cell: Cell; column: Column }): string {
    const precision = column.precision;
    const result = cell.number ? cell.number.toFixed(_.isNumber(precision) ? precision : this.DEFAULT_PRECISION) : '';
    return _.trim(result);
  }

  cellValueToNumber({ cell }: IColumnAndCell) {
    return cell.number;
  }

  cellValueToRating({ cell }: IColumnAndCell) {
    return Math.round(cell.number);
  }

  cellValueToCheckbox({ cell }: IColumnAndCell) {
    return !!cell.number;
  }

  cellValueToSelect({ column, cell }: IColumnAndCell) {
    const option = _.find(column.hasColumnOption.get(), i => Number(_.trim(i.name)) === cell.number);
    return option;
  }
  cellValueToMultiSelect({ cell, column }: IColumnAndCell) {
    const options = _.filter(column.hasColumnOption.get(), i => Number(_.trim(i.name)) === cell.number);
    return options;
  }
  setCellValue({ cell, action }: { cell: Cell; action: UpdateCellAction }) {
    const value = (action.value as NumberCellValue).number;
    cell.number = value;
    return this;
  }
  getCellValue(cell: Cell) {
    return cell.number;
  }

  convertCell(
    command: UpdateColumn_v2,
    o: CommonOptions,
    { columnCopy, referenceColumns }: { columnCopy: Column; referenceColumns?: Column[] },
  ) {
    const { column, columnConfig } = command;
    const { type: toType } = columnConfig;
    const { columnType: fromType } = columnCopy;

    const cells = column.hasCell.get();
    const SourceCells = columnCopy.hasCell.get();
    const events = new EventHelper();
    const fromCellHandle = this.instanceFactory.getCellInstance(fromType as ColumnType);
    if (this.needEmptyCell(command)) {
      fromCellHandle.emptyCells(command, cells, false, o);
    }
    if (cells.length) {
      const visableNameMapping = new Map<string, string>();
      _.forEach(cells, cell => {
        const action = {
          type: UpdateAction.SET_VALUE,
          value: { ...command.columnConfig, type: toType },
        };
        const updateCell = {
          ...command,
          action,
          _metadata: { ...command._metadata, source: Source.SAGA },
        };
        const value = action.value as NumberCellValue;
        value.number = fromCellHandle.cellValueToNumber({
          column: columnCopy,
          cell: SourceCells.find(c => c.uid === cell.uid)!,
        });
        const rowData = super.upsertCell(cell, { command: updateCell }, { ...o, events }, { columnCopy });
        if (rowData) {
          visableNameMapping.set(rowData.uid, rowData.visibleName);
        }
      });
      this.updateReferenceColumnAndDependencies({
        column,
        visableNameMapping,
        o,
        _metadata: command._metadata,
        referenceColumns,
      });
    }

    o.events.bulkAddEvents(...events);
    return this;
  }

  emptyCells(command: UpdateColumn_v2, cells: readonly Cell[], deleteCells: boolean, o: CommonOptions) {
    if (!deleteCells) {
      _.forEach(cells, cell => {
        o.delQuads.push(...this.dgraphHelperService.getRemoveObjectQuads(cell.uid!, 'number'));
      });
    }
    super.emptyCells(command, cells, deleteCells, o);
    return this;
  }

